/*
 * @Author: your name
 * @Date: 2020-08-20 19:23:51
 * @LastEditTime: 2020-08-28 16:30:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \react-exam\src\store\manager.ts
 */
/* 引入mobx  action:动作  observable：装饰器  （useObservable:使数据变为响应式）  */
import { observable, action } from 'mobx';

/* 引入antd */
import { message } from 'antd';

/* 引入获取所有教室接口api */
import { getRoomList, addRoom, delRoom, getAllRoomList,getNoRoomList,delAllRoomListItem, addClassManager, classManagerUpdate,allocatedList} from '../service/index';


import { AddClassItem} from '../utils/interface'; 

export default class Manager {
    [key: string]: any
    /* 教室号列表  (使用@observable时，在tsconfig.json里面设置 experimentalDecorators:true(消除警告)) */
    @observable
    roomList=[];

    /* 已经分配教室的班级列表 */
    @observable
    allRoomList=[];

    /* 获取没有分配教室的班级 */
    @observable
    noRoomList=[];

    /* 获取已分配学生 */
    @observable
    allocatedListItem=[]

    /* 获取教室号 */
    @action
    async getRoomListAction(){
        let result:any = await getRoomList();
        // console.log(result);

         /* 判断 */
         if (result.data.code===1) {
            /* 保存数据 */
            this.roomList = result.data.data;
        }
    };

    /* 添加教室号 */
    @action
    async addRoomAction(room_text: string){
        let result:any = await addRoom(room_text);
        /*  console.log(result); */

         /* 判断 */
          if (result.data.code===1) {
           this.getRoomListAction();
           message.success(result.data.msg)
        }
    };

    /* 删除教室号 */
    @action
    async delRoomAction(room_id: string){
        let result:any = await delRoom(room_id);
        /*  console.log(result); */

         /* 判断 */
          if (result.data.code===1) {
           this.getRoomListAction();
           message.success(result.data.msg)
        }
    };

    /* 获取已经分配教室的班级  */
    @action
    async getAllRoomListAction(){
        let result = await getAllRoomList();
        /* console.log('已经分配教室的班级',result.data) */
         /* 判断 */
         if (result.data.code===1) {
            /* 保存数据 */
            this.allRoomList = result.data.data;
           /*  console.log('待批',result.data) */
        }
    };

    /* 获取没有分配教室的班级 */
    @action
    async getNoRoomListAction(){
        let result = await getNoRoomList();
        // console.log(result);

         /* 判断 */
         if (result.data.code===1) {
            /* 保存数据 */
            this.noRoomList = result.data.data;
        }
    };

    /* 添加班级 */
    @action
    async addClassManagerAction( params: AddClassItem){
        
        
        let result=await addClassManager(params);
        /* console.log(' 添加班级...',result.data); */
        
         /* 判断 */
         if (result.data.code===1) {
            /* 调用已分配的班级数据与未分配的班级数据 */
            this.getAllRoomListAction();
            this.getNoRoomListAction();
            message.success(result.data.msg)
        }else{
            message.error(result.data.msg)
        }
    }

    /* 修改班级信息 */
    @action
    async classManagerUpdateAction(grade_id:string,grade_name?:string,subject_id?:string,room_id?:string){
        let result=await classManagerUpdate(grade_id,grade_name,subject_id,room_id);
        console.log(' 修改班级...',result.data);
        
         /* 判断 */
          /*if (result.data.code===1) {
            调用已分配的班级数据与未分配的班级数据 
            this.getAllRoomListAction();
            this.getNoRoomListAction();
        }else{
            message.error(result.data.msg)
        }*/
    }

     /* 删除班级 */
     @action
     async delAllRoomListItemAction(grade_id: string){
         let result:any = await delAllRoomListItem(grade_id);
         /*  console.log(result); */
 
          /* 判断 */
           if (result.data.code===1) {
            this.getAllRoomListAction();
            message.success(result.data.msg)
         }
     };

     /* 已分配学生 */
     @action
     async allocatedListAction(){
         let result=await allocatedList();
         /* console.log('qqqqqqqq',result.data); */
         
         if(result.data.code===1){
             this.allocatedListItem=result.data.data
         }
     }
}